package com.zz.parkingsystem.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.zz.parkingsystem.entity.Account;
import com.zz.parkingsystem.entity.User;
import com.zz.parkingsystem.mapper.AccountMapper;
import com.zz.parkingsystem.mapper.UserMapper;
import com.zz.parkingsystem.service.UserService;
import com.zz.parkingsystem.util.MD5Util;
import com.zz.parkingsystem.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import com.zz.parkingsystem.mapper.UserMapper;
import com.zz.parkingsystem.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public int deleteByPrimaryKey(Long id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }


    //登录
    @Override
    public User selByTelAndPassWord(User user) {
        User userByMysql = userMapper.selByTel(user.getUserTel());
        if(userByMysql!=null){
            String password = MD5Util.MD5Encode(user.getUserPassword(),userByMysql.getUserSalt(),"UTF-8");
            if(password.equals(userByMysql.getUserPassword())){
                userByMysql.setUserLoginTime(new Date());
                userMapper.updateByPrimaryKeySelective(userByMysql);
                return userByMysql;
            }
        }
        return null;
    }
    //注册 电话重复返回null,不重复注册成功，返回对象
    @Override
    public User addUser(User user) {
        User userByMysql = userMapper.selByTel(user.getUserTel());
        if(userByMysql!=null){
            return null;
        }
        if(!NumberUtil.isPhone(user.getUserTel())){
            return null;
        }
        String salt = RandomUtil.randomNumbers(6);
        user.setUserPassword(MD5Util.MD5Encode(user.getUserPassword(),salt,"UTF-8"));
        user.setUserSalt(salt);
        user.setUserCreatedTime(new Date());
        userMapper.insertSelective(user);
        //每注册一个用户，都有一个账户与之对应
        Account account = new Account();
        User userByMysqlAfterInsert = userMapper.selByTel(user.getUserTel());
        account.setUserId(userByMysqlAfterInsert.getUserId());
        account.setAccCarsBound(0);
        account.setAccSpend(new BigDecimal(0));
        account.setAccOrderCount(0);
        account.setAccUnpaid(new BigDecimal(0));
        accountMapper.insert(account);
        Account accountByMySql = accountMapper.selByUserId(userByMysqlAfterInsert.getUserId());
        user.setAccId(accountByMySql.getAccId());
        userMapper.updateByPrimaryKeySelective(user);
        return user;
    }

    @Override
    public int getselCountNumber(Date user_created_time) {
        return userMapper.selCountNumber(user_created_time);
    }
}
